package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页查询套餐列表
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO dto){
        log.info("分页查询套餐列表：{}",dto);
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")
    @PostMapping
    public Result  addSetmeal(@RequestBody SetmealDTO dto){
        log.info("新增套餐：{}",dto);
        setmealService.addSetmeal(dto);
        return Result.success();
    }

    /**
     * 修改套餐
     * @param dto
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PutMapping
    public Result update(@RequestBody SetmealDTO dto){
        log.info("修改套餐：{}",dto);
        setmealService.update(dto);
        return Result.success();
    }

    /**
     * 回显套餐列表
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("回显套餐：{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除套餐：{}",ids);
        setmealService.delete(ids);
        return Result.success();
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PostMapping("/status/{status}")
    public Result ebableDisable(@PathVariable("status") Integer status,Long id){
        setmealService.enableDisable(status,id);
        return Result.success();
    }
}
